package cn.app.android.sichuanxiaoke.com.xiaokeapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.activity.HomePHPActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.bean.Commodity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.ConstantUtils;

/**
 * Created by Administrator on 2017/9/3.
 */

public class HomeFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;

    public HomeFragmentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        int LayoutType = 0;
        switch (position){
            case 0:
                LayoutType=R.layout.home_fragment_viewpager;
                break;
            case 1:
            LayoutType=R.layout.home_fragment_calss;
            break;
            case 2:
            LayoutType=R.layout.home_fragment_heading_news;
            break;
            case 3:
            LayoutType=R.layout.home_fragment_goods_show;
            break;

        }
        return LayoutType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType){
            case R.layout.home_fragment_viewpager:
                 view = LayoutInflater.from(context).inflate(R.layout.home_fragment_viewpager, parent, false);

                return new ViewpagerViewHolder(view);
            case R.layout.home_fragment_calss:
                 view = LayoutInflater.from(context).inflate(R.layout.home_fragment_calss, parent, false);
                return new HomeclassViewHolder(view);
            case R.layout.home_fragment_heading_news:
                view = LayoutInflater.from(context).inflate(R.layout.home_fragment_heading_news, parent, false);
                return new HomeHeadingNewsViewHolder(view);
            case R.layout.home_fragment_goods_show:
                view = LayoutInflater.from(context).inflate(R.layout.home_fragment_goods_show, parent, false);
                return new HomeGoodsShowViewHolder(view);

        }
        return new ViewpagerViewHolder(view) ;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if( holder instanceof ViewpagerViewHolder ){
            ((ViewpagerViewHolder)holder).LoadView();
        }else if(holder instanceof HomeclassViewHolder){
            ((HomeclassViewHolder)holder).LoadView();
        }else if(holder instanceof HomeHeadingNewsViewHolder){
            ((HomeHeadingNewsViewHolder)holder).LoadView();
        }else if(holder instanceof HomeGoodsShowViewHolder){
            ((HomeGoodsShowViewHolder)holder).LoadView();
        }

    }

    @Override
    public int getItemCount() {
        return 4;
    }
    private class ViewpagerViewHolder extends RecyclerView.ViewHolder{
        RollPagerView rollPagerView;

        public ViewpagerViewHolder(View itemView) {
            super(itemView);
            rollPagerView= (RollPagerView) itemView.findViewById(R.id.roll_view_pager);
        }
        public void LoadView(){
            //设置播放时间间隔
            rollPagerView.setPlayDelay(2000);
            //设置透明度
            rollPagerView.setAnimationDurtion(500);
            //设置适配器
            rollPagerView.setAdapter(new TestNormalAdapter(context));

        }

    }

    private class HomeclassViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private RelativeLayout mRe1;
        private RelativeLayout mRe2;
        private RelativeLayout mRe3;
        private RelativeLayout mRe4;
        private RelativeLayout mRe5;
        private RelativeLayout mRe6;
        private RelativeLayout mRe7;
        private RelativeLayout mRe8;

        public HomeclassViewHolder(View itemView) {
            super(itemView);
            mRe1 = (RelativeLayout) itemView.findViewById(R.id.re_1);
            mRe2 = (RelativeLayout) itemView.findViewById(R.id.re_2);
            mRe3 = (RelativeLayout) itemView.findViewById(R.id.re_3);
            mRe4 = (RelativeLayout) itemView.findViewById(R.id.re_4);
            mRe5 = (RelativeLayout) itemView.findViewById(R.id.re_5);
            mRe6 = (RelativeLayout) itemView.findViewById(R.id.re_6);
            mRe7 = (RelativeLayout) itemView.findViewById(R.id.re_7);
            mRe8 = (RelativeLayout) itemView.findViewById(R.id.re_8);
        }

        public void LoadView(){
            mRe1.setOnClickListener(this);
            mRe2.setOnClickListener(this);
            mRe3.setOnClickListener(this);
            mRe4.setOnClickListener(this);
            mRe5.setOnClickListener(this);
            mRe6.setOnClickListener(this);
            mRe7.setOnClickListener(this);
            mRe8.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.re_1:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.re_2:
                    Intent intent=new Intent(context, HomePHPActivity.class);
                    intent.putExtra("URL", ConstantUtils.TEST_USER_RECH);
                    context.startActivity(intent);
                break;
                case R.id.re_3:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                break;
                case R.id.re_4:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                break;
                case R.id.re_5:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                break;
                case R.id.re_6:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();

                break;
                case R.id.re_7:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.re_8:
                    Toast.makeText(context,"暂时没开通，敬请期待",Toast.LENGTH_SHORT).show();
                    break;


            }
        }
    }

    private class HomeHeadingNewsViewHolder extends RecyclerView.ViewHolder{
        GridView gridLayout;
        public HomeHeadingNewsViewHolder(View itemView) {
            super(itemView);
            gridLayout= (GridView) itemView.findViewById(R.id.gradlayout);
        }

        public void LoadView(){
            gridLayout.setAdapter(new HomeFragmentHeadingNewsAdapter(context));
            gridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent=new Intent(context, HomePHPActivity.class);
                    intent.putExtra("URL", ConstantUtils.TEST_TEA_URL);
                    context.startActivity(intent);
                }
            });

        }
    }
    private class HomeGoodsShowViewHolder extends RecyclerView.ViewHolder{
        GridView gridview;
        public HomeGoodsShowViewHolder(View itemView) {
            super(itemView);
            gridview= (GridView) itemView.findViewById(R.id.gridview);
        }

        public void LoadView(){
            String URL1="http://pic10.nipic.com/20101003/3101644_140400043414_2.jpg";
            String URL2="http://pic28.nipic.com/20130425/2786001_140103647000_2.jpg";
            String URL3="http://img3.imgtn.bdimg.com/it/u=3971319814,1965386981&fm=214&gp=0.jpg";
            String URL4="http://pic.qiantucdn.com/58pic/13/92/99/05V58PICkuT_1024.jpg";
            String[] data={URL1,URL2,URL3,URL4};
            //int [] data={R.drawable.tr1,R.drawable.tr2,R.drawable.tr3,R.drawable.tr4};

            gridview.setAdapter(new HomeGoodsShowNewsAdapter(context,data));
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent;
                    switch (position){
                        case 0:
                             intent=new Intent(context, HomePHPActivity.class);
                            intent.putExtra("URL", ConstantUtils.TEST_TEA_URL);
                            context.startActivity(intent);
                            break;
                        case 1:
                             intent=new Intent(context, HomePHPActivity.class);
                            intent.putExtra("URL", ConstantUtils.TEST_TEA_URL);
                            context.startActivity(intent);
                            break;
                        case 2:
                             intent=new Intent(context, HomePHPActivity.class);
                            intent.putExtra("URL", ConstantUtils.TEST_TEA_URL5);
                            context.startActivity(intent);
                            break;
                        case 3:
                             intent=new Intent(context, HomePHPActivity.class);
                            intent.putExtra("URL", ConstantUtils.TEST_TEA_URL);
                            context.startActivity(intent);
                            break;
                    }

                }
            });



        }
    }
}
